<?php
namespace Orm\Region;

use Base\Exception\RmException;
class RegionClass extends \Base\BaseOrmObject{
    protected   $id,
				$RegionCode = '',
				$RegionName = '',
				$Parentid = '',
				$is_del = '',
				$Remark = '',
				$Parentcode = '',
				$ppCode = '',
				$active = '';

    function __init($data)
    {
        parent::__init($data);
        $this->dformat($data);
    }

    private function dformat()
    {
    }

    public function loadById($id = 0)
    {
        $model = new \Orm\Region\RegionModel();
        $data = $model->loadById($id);
        self::__init($data);
    }

    public function insert()
    {
        $this->checkNecessaryParam();
        $data = array(
            'RegionCode' => $this->RegionCode,
			'RegionName' => $this->RegionName,
			'Parentid' => $this->Parentid,
			'is_del' => $this->is_del,
			'Remark' => $this->Remark,
			'Parentcode' => $this->Parentcode,
			'ppCode' => $this->ppCode,
			'active' => $this->active,
        );

        $model = new \Orm\Region\RegionModel();
        $model->insert($data);
        $this->id = $model->getLastId();
        /******************************************************/
        parent::setBackupData(array_keys($data));
        $model->log($this->id, '新增', $this->backup_data);
        /******************************************************/
    }

    public function update()
    {
        $this->checkNecessaryParam();
        $data = array(
            'RegionCode' => $this->RegionCode,
			'RegionName' => $this->RegionName,
			'Parentid' => $this->Parentid,
			'is_del' => $this->is_del,
			'Remark' => $this->Remark,
			'Parentcode' => $this->Parentcode,
			'ppCode' => $this->ppCode,
			'active' => $this->active,
        );

        $model = new \Orm\Region\RegionModel();
        $model->update($data, "`id` = {$this->id}");
        /******************************************************/
        parent::setBackupData(array_keys($data));
        $model->log($this->id, '更新', $this->backup_data);
        /******************************************************/
    }

    public function delete($id)
    {
        $data = array('is_del'=>1);
        $model = new \Orm\Region\RegionModel();
        $model->update($data, "`id` = {$id}");
        /******************************************************/
        parent::setBackupData(array_keys($data));
        $model->log($this->id, '删除', $this->backup_data);
        /******************************************************/
    }

    private function checkNecessaryParam()
    {
    }
}